/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginPane.java
 *
 * Created on Jun 28, 2009, 3:35:29 PM
 */
package org.dbdir.client.gui;

import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import org.dbdir.Client;

/**
 *
 * @author thotheolh
 */
public class LoginPane extends javax.swing.JPanel {

    String usr;
    char[] pwdChar;
    Client client;
    MainFrame frame;

    /** Creates new form LoginPane */
    public LoginPane(Client client, MainFrame frame) {
        initComponents();
        setSize(285, 144);
        this.client = client;
        this.frame = frame;
    }

    private void doAuth() {
        usr = usrTf.getText();
        pwdChar = pwdTf.getPassword();
        String pwd = new String(pwdChar);
        boolean ok = false;
        ok = client.doLoginProtocol(usr, pwd);
        if (ok == true) {
            client.setUsername(usr);
            JOptionPane.showMessageDialog(null, "Welcome, " + usr + " .", "Welcome", JOptionPane.INFORMATION_MESSAGE);
            if (frame != null) {
                frame.getActionPane();
            }
        } else {
            this.showErrorMsg("Login FAILED. Try again.");
            usrTf.setText("");
            pwdTf.setText("");
            usrTf.grabFocus();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginContentPane = new javax.swing.JPanel();
        usrLbl = new javax.swing.JLabel();
        pwdLbl = new javax.swing.JLabel();
        usrTf = new javax.swing.JTextField();
        pwdTf = new javax.swing.JPasswordField();
        msgLbl = new javax.swing.JLabel();
        ok_cancel_pane = new javax.swing.JPanel();
        regBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        okBtn = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        usrLbl.setText("Username: ");

        pwdLbl.setText("Password: ");

        pwdTf.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwdTfKeyPressed(evt);
            }
        });

        msgLbl.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        msgLbl.setForeground(java.awt.Color.red);

        javax.swing.GroupLayout loginContentPaneLayout = new javax.swing.GroupLayout(loginContentPane);
        loginContentPane.setLayout(loginContentPaneLayout);
        loginContentPaneLayout.setHorizontalGroup(
            loginContentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginContentPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginContentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginContentPaneLayout.createSequentialGroup()
                        .addGroup(loginContentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(usrLbl)
                            .addComponent(pwdLbl))
                        .addGap(2, 2, 2)
                        .addGroup(loginContentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pwdTf, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                            .addComponent(usrTf, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)))
                    .addComponent(msgLbl))
                .addContainerGap())
        );
        loginContentPaneLayout.setVerticalGroup(
            loginContentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginContentPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginContentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usrLbl)
                    .addComponent(usrTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginContentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pwdLbl)
                    .addComponent(pwdTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(msgLbl)
                .addContainerGap())
        );

        add(loginContentPane, java.awt.BorderLayout.CENTER);

        ok_cancel_pane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        regBtn.setText("Register");
        regBtn.setToolTipText("Register an account");
        regBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regBtnActionPerformed(evt);
            }
        });
        ok_cancel_pane.add(regBtn);

        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });
        ok_cancel_pane.add(cancelBtn);

        okBtn.setText("Ok");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });
        ok_cancel_pane.add(okBtn);

        add(ok_cancel_pane, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        usrTf.setText("");
        pwdTf.setText("");
        usrTf.grabFocus();
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        // TODO: Login actions
        doAuth();
    }//GEN-LAST:event_okBtnActionPerformed

    private void pwdTfKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdTfKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            doAuth();
        }
    }//GEN-LAST:event_pwdTfKeyPressed

    private void regBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regBtnActionPerformed
        // TODO add your handling code here:
        frame.getAddUserPane();
    }//GEN-LAST:event_regBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JPanel loginContentPane;
    private javax.swing.JLabel msgLbl;
    private javax.swing.JButton okBtn;
    private javax.swing.JPanel ok_cancel_pane;
    private javax.swing.JLabel pwdLbl;
    private javax.swing.JPasswordField pwdTf;
    private javax.swing.JButton regBtn;
    private javax.swing.JLabel usrLbl;
    private javax.swing.JTextField usrTf;
    // End of variables declaration//GEN-END:variables

    private void showErrorMsg(String errStr) {
        msgLbl.setText("Error: " + errStr);
        msgLbl.setForeground(Color.RED);
    }

    private void showWarnMsg(String warnStr) {
        msgLbl.setText("Warning: " + warnStr);
        msgLbl.setForeground(Color.RED);
    }
}
